/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.struts.service.beans;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.struts.service.beans.JBMenu;
import opiam.admin.faare.struts.service.beans.JBMenuElement;
import opiam.admin.faare.struts.service.beans.JBProfileMenu;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.commons.collections.CollectionUtils;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Test of the JBProfileMenu.
 */
public class JBProfileMenuTest extends TestCase
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBProfileMenuTest.class);

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    /** Instance of JBProfileMenu. */
    private JBProfileMenu jbProfileMenu = new JBProfileMenu();

    /**
     * Creates a new JBProfileMenuTest object.
     *
     * @param arg0  Parameter.
     */
    public JBProfileMenuTest(String arg0)
    {
        super(arg0);

        try
        {
            PropertiesManager.getInstance();
        }
        catch (Exception e)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("JBProfileMenuTest constructor error\n");
            msg.append(e.toString());
            _test.displayResultFailure(msg.toString());
        }
    }

    /**
     * Method of the tests suite.
     *
     * @return Tests result.
     */
    public static Test suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new JBProfileMenuTest("testBegin"));

        suite.addTest(new JBProfileMenuTest("testGetName"));
        suite.addTest(new JBProfileMenuTest("testAddMenu"));
        suite.addTest(new JBProfileMenuTest("testFindMenuByName"));

        suite.addTest(new JBProfileMenuTest("testEnd"));

        return suite;
    }

    /**
     * Main method of the test class.
     *
     * @param args  Sets of the parameters.
     */
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * Method to indicate the beginning of the class test.
     *
     */
    public static void testBegin()
    {
        //  To initialize the tests statement
        TestResult.getInstance().setInitiator("JBProfileMenuTest");

        _test.displayHeaderClass("JBProfileMenuTest");
        assertTrue(true);
    }

    /**
     * Method to indicate the end of the class test.
     *
     */
    public static void testEnd()
    {
        _test.displayFooterClass(null);
        assertTrue(true);

        // To display the tests statement
        _test.displayTestStatement("JBProfileMenuTest");
    }

    /**
     * Test the methods: setName and getName.
     *
     */
    public void testGetName()
    {
        _test.displayHeaderMethod("testGetName");

        try
        {
            String expected = "profile_par_defaut";
            jbProfileMenu.setName("profile_par_defaut");

            String result = jbProfileMenu.getName();

            if (expected == result)
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetName");
    }

    /**
    * Test the methods: addMenuTest and getMenusMap.
    *
    */
    public void testAddMenu()
    {
        _test.displayHeaderMethod("testAddMenu");

        try
        {
            Map expectedMenusMap = new HashMap();
            Map resultMenusMap = new HashMap();

            JBMenu jbMenu = new JBMenu();
            JBMenuElement element1 = new JBMenuElement();

            jbMenu.setName("NewNenu");
            jbMenu.addElement(element1);

            expectedMenusMap.put("NewNenu", element1);

            int expectedSize = expectedMenusMap.size();

            jbProfileMenu.addMenu(jbMenu);
            resultMenusMap = jbProfileMenu.getMenusMap();

            int resultSize = resultMenusMap.size();

            if (expectedSize == resultSize)
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testAddMenu");
    }

    /**
     * Test the method: findMenuByName.
     *
     */
    public void testFindMenuByName()
    {
        _test.displayHeaderMethod("testFindMenuByName");

        try
        {
            JBMenu jbMenu1 = new JBMenu();
            JBMenu jbMenu2 = new JBMenu();
            JBMenuElement element1 = new JBMenuElement();
            JBMenuElement element2 = new JBMenuElement();

            jbMenu1.setName("Menu1");
            jbMenu1.addElement(element1);
            jbMenu2.setName("Menu2");
            jbMenu2.addElement(element2);

            jbProfileMenu.addMenu(jbMenu1);
            jbProfileMenu.addMenu(jbMenu2);

            JBMenu resultJbMenu1 = new JBMenu();
            JBMenu resultJbMenu2 = new JBMenu();

            resultJbMenu1 = jbProfileMenu.findMenuByName("Menu1");
            resultJbMenu2 = jbProfileMenu.findMenuByName("Menu2");

            List jbMenu1ElementsList = jbMenu1.getElements();
            List resultjbMenuElementsList = resultJbMenu1.getElements();

            CollectionUtils.isEqualCollection(jbMenu1ElementsList,
                resultjbMenuElementsList);

            if (resultJbMenu1.getName().equals(jbMenu1.getName()))
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testFindMenuByName");
    }
}
